﻿namespace Dyao.Book.Repository.Shared.EntityComplete
{
    public class EntityPropertyCompleteScope : IDisposable
    {
        private static AsyncLocal<EntityPropetyCompleteScopeValue> _asyncLocal = new AsyncLocal<EntityPropetyCompleteScopeValue>();

        private EntityPropetyCompleteScopeValue _last;

        public EntityPropertyCompleteScope(bool ignore = true) : this(new EntityPropetyCompleteScopeValue { Ignore = ignore })
        {
        }

        public EntityPropertyCompleteScope(EntityPropetyCompleteScopeValue scopeValue)
        {
            _last = _asyncLocal.Value;

            _asyncLocal.Value = scopeValue;
        }

        public static EntityPropetyCompleteScopeValue Current => _asyncLocal.Value;

        public void Dispose()
        {
            _asyncLocal.Value = _last;
        }
    }

    public struct EntityPropetyCompleteScopeValue
    {
        /// <summary>
        /// 忽略操作
        /// </summary>
        public bool Ignore;
    }
}
